﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using DBAccessLibrary.Model;
using DBAccessLibrary.Common;
using System.Collections.ObjectModel;
using DevExpress.Xpf.Core;
using DevControlLibrary.Common;
using QueryResource.Common;

namespace DevControlLibrary.UI.Comparsion
{
    /// <summary>
    /// DataFilterColumnWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DataFilterColumnWindow : DXWindow
    {
        public DataFilterColumnWindow(string tablename)
        {
            InitializeComponent();
            this.DataContext = this;
            this.lbTableName.Content = tablename;
            DataFilterLeft = new ObservableCollection<DataFilterColumnEntity>();
            DataFilterRight = new ObservableCollection<DataFilterColumnEntity>();
            XmlHelper.Instance.SetTheme(this);
        }

        public ObservableCollection<DataFilterColumnEntity> DataFilterLeft { get; set; }
        public ObservableCollection<DataFilterColumnEntity> DataFilterRight { get; set; }


        public void DataBind(List<FieldEntity> list)
        {
            foreach (FieldEntity entity in list)
            {
                DataFilterLeft.Add(new DataFilterColumnEntity { 
                    ColumnName = entity.Name,
                    Primary = entity.IsPrimaryKey ? "PK" :"",
                    TypeName = entity.TypeName.ToUpper(),
                    IsNull = entity.IsNull
                });
            }
            ShowState();
        }

        private void ShowState()
        {
            if (DataFilterLeft.Count == 0)
            {
                this.btnAlltoLeft.IsEnabled = true;
                this.btntoLeft.IsEnabled = true;
                this.btnAlltoRight.IsEnabled = false;
                this.btntoRight.IsEnabled = false;
            }
            if (DataFilterRight.Count == 0)
            {
                this.btnAlltoRight.IsEnabled = true;
                this.btntoRight.IsEnabled = true;
                this.btnAlltoLeft.IsEnabled = false;
                this.btntoLeft.IsEnabled = false;
            }
            if (DataFilterLeft.Count > 0 && DataFilterRight.Count > 0)
            {
                this.btnAlltoRight.IsEnabled = true;
                this.btntoRight.IsEnabled = true;
                this.btnAlltoLeft.IsEnabled = true;
                this.btntoLeft.IsEnabled = true;
            }
        }
        #region tool
        private void btntoRight_Click(object sender, RoutedEventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                DataFilterColumnEntity columnEntity = listBox1.SelectedItem as DataFilterColumnEntity;
                if (columnEntity != null)
                {
                    DataFilterRight.Add(columnEntity);
                    DataFilterLeft.Remove(columnEntity);
                }
                ShowState();
            }
        }

        private void btntoLeft_Click(object sender, RoutedEventArgs e)
        {
            if (listBox2.SelectedItem != null)
            {
                DataFilterColumnEntity columnEntity = listBox2.SelectedItem as DataFilterColumnEntity;
                if (columnEntity != null)
                {
                    DataFilterLeft.Add(columnEntity);
                    DataFilterRight.Remove(columnEntity);
                }
                ShowState();
            }
        }

        private void btnAlltoRight_Click(object sender, RoutedEventArgs e)
        {
            foreach (DataFilterColumnEntity entity in DataFilterLeft)
            {
                DataFilterRight.Add(entity);
            }
            DataFilterLeft.Clear();
            ShowState();
        }

        private void btnAlltoLeft_Click(object sender, RoutedEventArgs e)
        {
            foreach (DataFilterColumnEntity entity in DataFilterRight)
            {
                DataFilterLeft.Add(entity);
            }
            DataFilterRight.Clear();
            ShowState();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (DataFilterRight.Count == 0)
            {
                MessageBoxHelper.Show("请选择要对比的列？");
                return;
            }

            this.DialogResult = true;
        }

        private void btnCanel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        #endregion

        private void DXWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnOK_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            { btnCanel_Click(null, null); }
        }
    }


    public class DataFilterColumnEntity : BasePropertyChange
    {
        private string _columnName;
        private string _primaryStr;
        private string _typeName;
        private bool _isnull;
        public string ColumnName
        {
            get { return _columnName; }
            set
            {
                if (value != _columnName)
                {
                    _columnName = value;
                    this.NotifyPropertyChanged(o => o.ColumnName);
                }
            }
        }

        public string Primary
        {
            get { return _primaryStr; }
            set
            {
                if (value != _primaryStr)
                {
                    _primaryStr = value;
                    this.NotifyPropertyChanged(o => o.Primary);
                }
            }
        }

        public string TypeName
        {
            get { return _typeName; }
            set
            {
                if (value != _typeName)
                {
                    _typeName = value;
                    this.NotifyPropertyChanged(o => o.TypeName);
                }
            }
        }

        public bool IsNull
        {
            get { return _isnull; }
            set
            {
                if (value != _isnull)
                {
                    _isnull = value;
                    this.NotifyPropertyChanged(o => o.IsNull);
                }
            }
        }
    }
}
